//
//  LBXDownLoadManager.h
//  LBXKit
//
//  Created by lbx on 2017/11/2.
//  Copyright © 2017年 lbx. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "LBXDownLoadItem.h"


@protocol LBXDownLoadManagerProtocol <NSObject>

@optional
//下载完成通知
- (void)LBXDownLoadSucess:(LBXDownLoadItem*)item;


//自定义请求,自定义设置相关协议头等等
- (NSURLRequest*)LBXCustomNSURLRequest:(LBXDownLoadItem*)item;

//硬盘空间不足，所有下载暂停
- (void)LBXDownLoadDiskSpaceNotAvailable;

@end

@interface LBXDownLoadManager : NSObject

@property (nonatomic, weak) id<LBXDownLoadManagerProtocol> delegate;



//特殊要求，可自行配置
@property (nonatomic, strong,readonly) NSURLSessionConfiguration *config;
//允许蜂窝访问，默认YES
@property (nonatomic, assign) BOOL allowsCellularAccess;

@property (nonatomic, strong,readonly) NSURLSession *session;

//最大同时下载个数，默认值5
@property (nonatomic, assign) NSInteger maxConcurrentCount;

//待下载列表，下载完成后，不会在这个列表中
@property (nonatomic, strong,readonly)  NSMutableArray<LBXDownLoadItem*> *downLoadItems;

//已下载列表,存储下载地址
@property (nonatomic, strong,readonly)  NSMutableArray<NSString*> *hasDownLoadItems;


//网络状态
//@property(readonly,nonatomic,assign)NetWorkStatus networkState;
//回调网络状态
//@property (nonatomic, copy) void (^listenNetBlock)();

+ (instancetype)sharedManager;

//放到缓存队列中
- (void)addItem:(LBXDownLoadItem*)item;

// 开始下载
- (void)startWithItem:(LBXDownLoadItem*)item;

//暂停下载
- (void)suspendWithItem:(LBXDownLoadItem*)item;

//恢复下载,和startWithItem功能一样
- (void)resumeWithItem:(LBXDownLoadItem*)item;

//取消下载,就是删除
- (void)cancelWithItem:(LBXDownLoadItem*)item;

//根据状态，自动开始下载或暂停下载
- (void)startOrSuspendItem:(LBXDownLoadItem*)item;

//自动下一个缓存开始
- (void)autoDownNextItem;

//暂停所有
- (void)suspendAll;

//全部开始，只是指定最多同时下载个数开始下载，其他准备设置为准备状态
- (void)startAll;

//取消所有下载，删除相应记录
- (void)cancelAll;

//清除所有回调block，在下载界面隐藏时调用
- (void)clearAllBlock;

//移除已经下载完成的item
- (void)clearFinishItem;

//有wifi启动下载
- (void)startNet;

- (NSArray*)loadItems;
- (void)storeItems;

/// 根据下载地址获取本地缓存地址
/// @param remoteURL 如果有值表示已经下载该文件，nil:表示该文件未下载或未下载完成
- (NSString*)localFilePathWithRemoteURL:(NSString*)remoteURL;

//删除已经下载的文件
- (void)deleteFileWithRemoteURL:(NSString*)remoteURL;

//删除所有已下载的文件
- (void)deleteAllDownedFiles;

//检查已下载文件记录，如果实际不存在，则删除记录
- (void)checkHasDownLoadItems;

@end
